<?php

function userGetToAjax(&$u, $level = 0)
{
	$o = array(
		'id' => $u['id'],
		'hi' => $u['hi'],
		'parent_id' => $u['parent_id'],
		'status' => $u['status'],
		'level' => $u['level'],
		'fio' => $u['fio'],
		'email' => $u['email'],
		'date' => $u['day'].".".$u['month'].".".$u['year'],
		'otkuda' => $u['otkuda'],
		'skype' => $u['skype'],
		'phone' => $u['phone'],
		'icq' => $u['icq'],
		'ref_email' => $u['referer_email'],
		'ref_fio' => $u['referer_fio'],
		'otstup' => ($level * 15),
		'can_delete' => 1
	);
	return $o;
}

function &userGetArrayIndexByStructure($structure)
{
	$out = array();
	foreach($structure as $u)
	{
		$out[$u['id']] = $u;
		if(@$u['children'] && count($u['children']))
		{
			$arr_index = userGetArrayIndexByStructure($u['children']);
			foreach($arr_index as $u2)
			{
				$out[$u2['id']] = $u2;
			}
		}
	}
	return $out;
}


function userCanChangeLevel($user_id)
{
	$CI = &get_instance();
	static $root;
	if(!@$root)
	{
		$root = $CI->MUser->getRoot();
	}
	if($root['id'] == Auth::$user_id || Auth::isSupport()) return true;
	return $user_id != Auth::$user_id && $this->MUser->inMyStructure($user_id);
}

function userCanChangeParentUser($user_id)
{
	return userCanChangeLevel($user_id);
}